// This is a texture sampler.  It lets you sample textures!  The keyword
// "uniform" means constant - sort of.  The uniform variables are the same
// for all fragments in an object, but they can change in between objects.
uniform sampler2D diffuseMap;
uniform sampler2D specularMap;
uniform sampler1D celShadeMap;
//uniform sampler1D celGreenShadeMap;
//uniform sampler1D celBlueShadeMap;
uniform sampler2D shadowMap;

// Diffuse, ambient, and specular materials.  These are also uniform.
uniform vec3 Kd;
uniform vec3 Ks;
uniform vec3 Ka;
uniform float alpha;

// These are values that OpenGL interpoates for us.  Note that some of these
// are repeated from the fragment shader.  That's because they're passed
// across.
varying vec2 texcoord;
varying vec3 normal;
varying vec3 eyePosition;
varying vec4 shadowCoord;

void main() {

	// Normalize the normal, and calculate light vector and view vector
	// Note: this is doing a directional light, which is a little different
	// from what you did in Assignment 2.
	vec3 N = normalize(normal);
	vec3 L = normalize(gl_LightSource[0].position.xyz);
	vec3 V = normalize(-eyePosition);
		
	// Calculate the diffuse color coefficient, and sample the diffuse texture
	//float Rdiff = max(0.0, dot(L, N));
        float Rd = 1.0; // get rid of the L, N term
	bool backface = false; 
        if (dot(L,N) <= 0.0) backface = true;
	vec3 Td = texture2D(diffuseMap, texcoord).rgb;

	vec3 diffuse = Rd * Kd * Td * gl_LightSource[0].diffuse.rgb;
/*
	if (Rdiff > 0.95) {
		vec3 mul = vec3(0.8,0.8,0.8);
		diffuse = diffuse * mul;	
	} else if (Rdiff > 0.5) {
		vec3 mul = vec3(0.5,0.5,0.5);
		diffuse = diffuse * mul;		
	} else if (Rdiff > 0.25) {
		vec3 mul = vec3(0.25,0.25,0.25);
		diffuse = diffuse * mul;	
	} else {
		vec3 mul = vec3(0.1,0.1,0.1);
		diffuse = diffuse * mul;		
	}
*/
	//diffuse.r = floor(diffuse.r * 10) / 10;
	//diffuse.g = floor(diffuse.g * 10) / 10;
	//diffuse.b = floor(diffuse.b * 10) / 10;

	//diffuse = diffuse * texture1D(celShadeMap, Rdiff);
	// Ambient
	vec3 ambient = Ka * gl_LightSource[0].ambient.rgb;
        // Copy the diffuse color to ambient
       
	// Calculate the specular coefficient
	vec3 R = reflect(-L, N);
	float Rs = pow(max(0.0, dot(V, R)), alpha);
	vec3 Ts = texture2D(specularMap, texcoord).rgb;
	vec3 specular = Rs * Ks * Ts * gl_LightSource[0].specular.rgb;
		
  	vec3 color = diffuse + ambient;
	
	if (color.r >0.8) {
		color.r = 0.8;	
	} else if (color.r > 0.6) {
		color.r = 0.6;	
	} else if (color.r > 0.4) {
		color.r = 0.4;	
	} else if (color.r > 0.2) {
		color.r = 0.2;	
	} else {
		color.r = 0.1;	
	}

	if (color.g >0.8) {
		color.g = 0.8;	
	} else if (color.g > 0.6) {
		color.g = 0.6;	
	} else if (color.g > 0.4) {
		color.g = 0.4;	
	} else if (color.g > 0.2) {
		color.g = 0.2;	
	} else {
		color.g = 0.1;	
	}

	if (color.b >0.8) {
		color.b = 0.8;	
	} else if (color.b > 0.6) {
		color.b = 0.6;	
	} else if (color.b > 0.4) {
		color.b = 0.4;	
	} else if (color.b > 0.2) {
		color.b = 0.2;	
	} else {
		color.b = 0.1;	
	}
	color = color + specular;

	gl_FragColor = vec4(color, 1);

    	vec4 shadowCoordinateWdivide = shadowCoord / shadowCoord.w ;
	shadowCoordinateWdivide.z += 0.005;
	float distanceFromLight = texture2D(shadowMap, shadowCoordinateWdivide.st).z;
 	float shadow = 1.0;
 	if (shadowCoord.w > 0.0) {
		shadow = ((distanceFromLight <shadowCoordinateWdivide.z)|| backface)? 0.4 : 1.0;	
	}
        
	gl_FragColor = shadow * gl_FragColor;
    
}
